﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CefSharp.WinForms.Vue3.Tool
{
    /// <summary>
    /// 调用web的方法
    /// </summary>
    public class WebFun
    {
        ChromiumWebBrowser ownerBrowser;
        public WebFun(CefSharp.WinForms.ChromiumWebBrowser OwnerBrowser)
        {
            ownerBrowser = OwnerBrowser;
        }
        public void Message()
        {
            try
            {
                string val = "后台调用信息:liuguang";
                //调用挂载到window上的方法
                ownerBrowser.EvaluateScriptAsync("window.webMethod.sendMessage('" + val + "')");
                //在 CefSharp 中触发事件
                ownerBrowser.EvaluateScriptAsync(@"
    const event = new CustomEvent('myCustomEvent', {
        detail: { type:'msg',code:'200' ,message: 'Hello from C#' }
    });
    window.dispatchEvent(event);
");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
